package net.gyapp.markim.module.collect;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.manager.ImageManager;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.entity.MemberEntity;
import net.gyapp.markim.dao.DBManager;
import net.gyapp.markim.dao.MemberEntityDao;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.AccountChanged;
import net.gyapp.markim.module.basic.event.AccountIml;
import net.gyapp.markim.module.contact.InfoSetActivity;
import net.gyapp.markim.module.contact.vo.UserVo;
import net.gyapp.markim.module.message.ConversationActivity;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/31
 * Version: 1.0
 * Desc:
 */


public class UserIntroduceActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_grade)
    ImageView ivGrade;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    public final static int TYPE_MEMBER = 10001;
    public final static int TYPE_USER = 10002;

    MemberEntity member;
    UserVo user;
    int type;
    String memberId;

    public static Intent getIntent(Context context, UserVo user) {
        return new Intent(context, UserIntroduceActivity.class).putExtra("user", user).putExtra("type", TYPE_USER);
    }

    public static Intent getIntent(Context context, MemberEntity member) {
        return new Intent(context, UserIntroduceActivity.class).putExtra("member", member).putExtra("type", TYPE_MEMBER);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_user_introduce;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("详细资料");
        type = intent.getIntExtra("type", TYPE_MEMBER);
        switch (type) {
            case TYPE_MEMBER:
                member = (MemberEntity) intent.getSerializableExtra("member");
                ImageManager.load(mActivity, ivHeader, member.avatar, R.drawable.placeholder_header);
                if (!TextUtils.isEmpty(member.remark)) {
                    tvName.setText(member.remark);
                } else {
                    tvName.setText(TextUtils.isEmpty(member.nick) ? member.memberId : member.nick);
                }
                tvId.setText(String.format("%1$s%2$s", "ID：", member.memberId));
                memberId = member.memberId;
                if (!ImHelper.isFriend(member.memberId)) {
                    btnConfirm.setText("添加到通讯录");
                }
                break;
            case TYPE_USER:
                user = (UserVo) intent.getSerializableExtra("user");
                if (user.userInfo.userName.equals(ImHelper.getSelf())) {
                    btnConfirm.setVisibility(View.GONE);
                }
                ImageManager.load(mActivity, ivHeader, user.userInfo.userAvatar, R.drawable.placeholder_header);
                if (!TextUtils.isEmpty(user.userInfo.remark)) {
                    tvName.setText(user.userInfo.remark);
                } else {
                    tvName.setText(TextUtils.isEmpty(user.userInfo.nick) ? user.userInfo.userName : user.userInfo.nick);
                }
                memberId = user.userInfo.userName;
                tvId.setText(String.format("%1$s%2$s", "ID：", user.userInfo.userName));
                if (!ImHelper.isFriend(user.userInfo.userName)) {
                    btnConfirm.setText("添加到通讯录");
                } else {
                    btnConfirm.setText("发消息");
                }
                break;
        }
        String s = tvName.getText().toString();
        if (TextUtils.isEmpty(s)) {
            tvName.setText("暂无昵称");
        }

        String confirm = btnConfirm.getText().toString();
        if (btnConfirm.getVisibility() == View.VISIBLE && !TextUtils.isEmpty(confirm) && confirm.equalsIgnoreCase("发消息")) {
            titleView.title_iv_right.setBackgroundResource(R.drawable.ic_more);
            titleView.title_iv_right.setVisibility(View.VISIBLE);
            titleView.title_iv_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (member != null) {
                        startActivity(InfoSetActivity.getIntent(mActivity, member));
                    } else if (user != null) {

                    }
                }
            });
        }
    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if (url.contains(ApiConfig.API_ADD_USER_FRIEND)) {
            showToast("申请已发送");
            finish();
        }
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }


    @OnClick({R.id.btn_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                switch (type) {
                    case TYPE_MEMBER:
                        if (ImHelper.isFriend(member.memberId)) {
                            List<MemberEntity> list = DBManager.newInstance().getDaoSession().getMemberEntityDao().queryBuilder().where(MemberEntityDao.Properties.MemberId.eq(member.memberId), MemberEntityDao.Properties.UserId.eq(ImHelper.getSelfUserId())).list();
                            if (list != null && list.size() > 0) {
                                startActivity(ConversationActivity.getIntent(mActivity, ImHelper.generateConversation(list.get(0))));
                            }
                        } else {
                            //添加好友
                            addFriend(member.memberId);
                        }
                        break;
                    case TYPE_USER:
                        if (ImHelper.isFriend(user.userInfo.userName)) {
                            List<MemberEntity> list = DBManager.newInstance().getDaoSession().getMemberEntityDao().queryBuilder().where(MemberEntityDao.Properties.MemberId.eq(user.userInfo.userName), MemberEntityDao.Properties.UserId.eq(ImHelper.getSelfUserId())).list();
                            if (list != null && list.size() > 0) {
                                startActivity(ConversationActivity.getIntent(mActivity, ImHelper.generateConversation(list.get(0))));
                            }
                        } else {
                            //添加好友
                            addFriend(user.userInfo.userName);
                        }
                        break;
                }

                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void accountChanged(AccountChanged changed) {
        if (changed.sign == AccountIml.ACCOUNT_CONTACT_INFO_CHANGED) {
            if (!TextUtils.isEmpty(memberId)) {
                MemberEntity entity = ImHelper.queryMemberEntity(memberId);
                if (entity != null && !TextUtils.isEmpty(entity.remark)) {
                    tvName.setText(entity.remark);
                    if (member != null) {
                        member.remark = entity.remark;
                    }

                    if (user != null) {
                        user.userInfo.remark = entity.remark;
                    }


                }
            }
        }
    }

    private void addFriend(String memberId) {
        presenter.setNeedDialog(true);
        presenter.postData(ApiConfig.API_ADD_USER_FRIEND, new RequestParams().put("friendId", memberId).get(), BaseVo.class);
    }
}
